// pages/index/home/home.js
Page({
  /**
   * 页面的初始数据
   */
  data: {

    type1: 0,

    list: '',
    taglist: [],
    tagsName: [],
    facility: [],
    facilitys: [],
    type: 0,

    houseId: 0,
    userId: parseInt(wx.getStorageSync('userInfo').userId, 10), // 转换为整数类型,
    fangdongid: 0,
    fangdongname: '',
    job: [],
    jobList: [],
    id: '',
    isClick: false,
    jobStorage: [],
    jobId: '',
    imageList: [{
        src: "../../../images/woshi1.jpg"
      },
      {
        src: "../../../images/woshi2.jpg"
      },
      {
        src: "../../../images/keting1.jpg"
      },
      {
        src: "../../../images/keting2.jpg"
      }
    ],
    markers: [{ // 绘制浮标，传入JSON支持多个
      iconPath: "../../../images/location.png", //浮标图片路径，推荐png图片
      id: 0, // Id支持多个，方便后期点击浮标获取相关信息
      latitude: 23.099994, // 经度
      longitude: 113.324520, //纬度
      width: 50, // 浮标宽度
      height: 50 // 浮标高度
    }],
    myLocation: ''
  },
  navigateToReport() {
    // 存储数据到本地缓存
    wx.setStorageSync('houseId', this.data.houseId);
    wx.setStorageSync('userId', this.data.userId);
    // 导航到目标页面
    wx.navigateTo({
      url: '/pages/index/home/report/report?type=' + this.data.type
    });
  },
  //收藏按钮
  haveSave(e) {
    // console.log(e)
    if (!this.data.isClick == true) {
      let jobData = this.data.jobStorage;
      jobData.push({
        jobid: jobData.length,
        id: this.data.job.id
      })
      wx.setStorageSync('jobData', jobData); //设置缓存
      wx.showToast({
        title: '已收藏',
      });

      wx.request({
        url: 'http://localhost:8080/ihome/collect',
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          houseIdOrTopicId: this.data.houseId,
          userId: this.data.userId,
          collectType: this.data.type1,
        },
        method: 'POST',
        success(resp) {
          // console.log(resp);
        }
      });

    } else {
      wx.request({
        url: 'http://localhost:8080/ihome/collect',
        data: {
          houseIdOrTopicId: this.data.houseId,
          userId: this.data.userId,
          collectType: this.data.type1,
        },
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        // data: {
        //   userHouseId: this.data.houseId,
        // },
        method: "DELETE",
        success(resp) {
          // console.log(resp);
        }
      });
      wx.showToast({
        title: '已取消收藏',
      });


    }
    this.setData({
      isClick: !this.data.isClick
    })
  },

  handleImageTap: function (e) {
    wx.navigateTo({
      url: '/pages/index/home/allimage/allimage', // 替换为你要跳转的新页面路径
    });
  },
  Tobooking: function (e) {
    wx.navigateTo({
      url: '/pages/index/home/bookinghome/bookinghome',
    });
  },
  Tocontact: function (e) {
    wx.navigateTo({
      url: "/pages/index/home/onlincontact/onlincontact?targetUserId="+this.data.fangdongname
    });
  },
  Tomore: function (e) {
    wx.navigateTo({
      url: '/pages/index/wholeRent/wholeRent',
    });
  },
  markertap(e) { // 这是一个事件，在wxml中绑定这个事件，点击浮标后
    wx.openLocation({ //此设置属于高级APi,可以打开微信内置地图组件
      latitude: 23.099994,
      longitude: 113.324520,
    });
  },
  onClickIcon() {
    wx.Toast('点击图标');
  },

  onClickButton() {
    Toast('点击按钮');
  },
  checkCollectionStatus() {
    wx.request({
      url: 'http://localhost:8080/ihome/collect/list',
      data: {
        // JSON.stringify(that.data.myForm)
        // houseId:JSON.stringify(this.data.houseId) ,
        // userId: JSON.stringify(this.data.userId),
        // collectType: JSON.stringify(this.data.type1)

        houseIdOrTopicId: this.data.houseId,
        userId: this.data.userId,
        collectType: this.data.type1
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (resp => {
        // console.log(resp)
        if (resp.data.total != 0) {
          this.setData({
            isClick: true
          });
        } else {
          this.setData({
            isClick: false
          });
        }
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    let that = this
    wx.request({
      url: 'http://localhost:8080/ihome/home/' + options.id,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (resp) => {
        // console.log(resp)
        that.setData({
          list: resp.data.data,
          taglist: resp.data.data.taglist,
          fangdongid: resp.data.data.sysUser.userId
        })
        this.toTag(resp.data)
        this.toFacility(resp.data)
      }
    })


    // wx.request({
    //   url: 'http://localhost:8080/ihome/home/house' + options.id,
    //   method: 'GET',
    //   header: {
    //     'Authorization': 'Bearer ' + wx.getStorageSync('token')
    //   },
    //   success: (resp) => {
    //     console.log(resp)
    //     that.setData({
    //       fangdongname:resp.data.data.sysUser.
    //     })
    //   }
    // })


    that.setData({
      houseId: options.id,
      fangdongname:options.nickName
    })
    // console.log(this.data.houseId)
    this.checkCollectionStatus(); // 检查收藏状态
  },
  toTag: function (data) {
    var that = this
    var size = data.data.taglist.length
    var tags = data.data.taglist
    let tagsName = that.data.tagsName
    // console.log(data)
    for (var i = 0; i < size; i++) {
      wx.request({
        url: 'http://localhost:8080/ihome/tag/' + tags[i],
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        success: (resp) => {
          // console.log(resp.data.data.tagName)
          // tagsName.push(resp.data.data.tagName)
          that.setData({
            tagsName: tagsName
          })
        }
      })
    }
  },
  toFacility: function (data) {
    var that = this
    // console.log(data)
    wx.request({
      url: 'http://localhost:8080/ihome/HomeAndFacility/list',
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        houseId: data.data.id
      },
      success: (resp) => {
        let facilitys = [];
        for (var i = 0; i < resp.data.rows.length; i++) {
          facilitys.push(resp.data.rows[i].facilityId)
        }
        that.setData({
          facility: facilitys
        })
        this.toFacilitys(resp.data)
      }
    })
  },
  toFacilitys: function (data) {
    // console.log(data)
    let facilitys = []
    for (var i = 0; i < data.rows.length; i++) {
      wx.request({
        url: 'http://localhost:8080/ihome/roomFacility/' + data.rows[i].facilityId,
        method: 'GET',
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        success: (resp) => {
          // console.log(resp)
          facilitys.push(resp.data.data)
          this.setData({
            facilitys: facilitys
          })

        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})